/**
 * @公司:公约树
 * @位置:苏州
 * @Classname WindowFocusUtil
 * @Date 2022-05-27 下午 05:26
 * @Created by hanWanLu
 * @auther 中文:韩万路
 */


package com.animeJava.util;

import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.stage.Window;

/**
 * 窗口焦点工具
 */
public class WindowFocusUtil {

    private Node[] parentControl;
    private Window window;

    private WindowFocusEvent windowFocusEvent;

    /**
     * window的事件接口
     * @param window        当前窗口的焦点
     * @param parentControl 忽略谁的焦点
     */
    public WindowFocusUtil(Window window, Node... parentControl ) {
        this.parentControl = parentControl;
        this.window = window;

        Platform.runLater(()-> {
            init();
        });
    }
    private void init(){
        if(parentControl[0] != null){
            Scene scene = parentControl[0].getScene();
            Window window = scene.getWindow();
            //获取当前窗口焦点的事件
            scene.addEventHandler(EventType.ROOT, new EventHandler<Event>() {
                @Override
                public void handle(Event event) {
                    EventType<? extends Event> eventType = event.getEventType();
                    if((eventType.getName().equals("MOUSE_PRESSED") )  && windowFocusEvent != null){
                        MouseEvent mouseEvent =(MouseEvent) event;
                        MouseButton button = mouseEvent.getButton();
                        if(button == MouseButton.PRIMARY){
                            //失去焦点
                            windowFocusEvent.run("失去焦点");
                        }
                    }
                }
            });
            window.xProperty().addListener(new ChangeListener<Number>() {
                @Override
                public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                    if( windowFocusEvent != null)
                        windowFocusEvent.run("窗口移动X");
                }
            });
            window.yProperty().addListener(new ChangeListener<Number>() {
                @Override
                public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                    if( windowFocusEvent != null)
                        windowFocusEvent.run("窗口移动Y");
                }
            });
            window.heightProperty().addListener(new ChangeListener<Number>() {
                @Override
                public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                    if( windowFocusEvent != null)
                        windowFocusEvent.run("窗口大小Height");
                }
            });
            window.widthProperty().addListener(new ChangeListener<Number>() {
                @Override
                public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                    if( windowFocusEvent != null)
                        windowFocusEvent.run("窗口大小Width");
                }
            });
        }
    }
    public void setWindowFocusEvent(WindowFocusEvent windowFocusEvent){
        this.windowFocusEvent = windowFocusEvent;
    }
    /**
     * 事件接口
     */
    public interface WindowFocusEvent{
        void run(String msg);
    }

}
